home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE13 / COMPRESS / COMPRESS.ZIP / SELFEXTF.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-09  |  4.3 KB  |  121 lines

  1. unit Selfextf;
  2. (* TCompress V2.5 -- Self-Extracting-EXE example program
  3.   This provides instructions and working code examples for:
  4.     a) Creating a compressed archive as a resource
  5.     b) Loading the compressed resource using LoadCompressedResource
  6.     c) Expanding the compressed archive into a directory with ExpandFilesFromStream
  7.  
  8.   Notes:
  9.   a) For an example which compresses and loads individual bitmaps, see BMTEST.DPR
  10.   b) For an example with NO forms overhead (but hence no user interface), see SELFXSML.DPR
  11.   c) This code has been tested satisfactorily with Delphi 2.0. With
  12.      Delphi 1.02 we encountered exceptions on ONE of our test systems
  13.      (Windows95 with Service Pack 1 and SP1 patches installed) which we have not
  14.      been able to eliminate after substantial tracing and testing.
  15.      Your mileage may vary.
  16.  
  17.   INSTRUCTIONS
  18.   ============
  19.   Part A: Creating a compressed archive as a resource:
  20.   1. Use COMPDEMO to make a new compressed archive (e.g. MYARCHIV.ARC) containing
  21.      all the files you want to automatically install (try NOTEPAD.EXE and a
  22.      simple README.TXT for example)
  23.   2. Create a text file called COMP_RES.RC containing the following (where
  24.      any reference to MYARCHIV is replaced with the correct name of your archive):
  25. /* Compile this file with (16-bit): delphi\bin\brc -r COMP_RES.RC
  26.    or                     (32-bit): delphi2.0\bin\brc32 -r COMP_RES.RC
  27.    That will create a COMP_RES.RES file which should be included in the main
  28.    unit of your project.
  29. */
  30. MyArchiv TCOMPRESS "MyArchiv.arc"
  31.    (you can have additional lines for other resources if necessary)
  32.  
  33.   3. Compile the resource file per the instructions in its header.
  34.      Note the {$R COMP_RES.RES} reference to it in this file (just after
  35.      the start of the 'implementation' section.
  36.   4. Alter the PROG_NAME and README_NAME constants below to
  37.      refer to the correct files (they're optional, so blank is ok)
  38.  
  39.   Part B: Loading the compressed resource using LoadCompressedResource
  40.   5. Refer to the code in DOINSTAL.PAS -- it is called when the INSTALL button
  41.   on this form is pushed. Change the RESOURCE_NAME constant to be the correct
  42.   name of your resource (e.g. MyArchiv).
  43.   6. The resource loading (in compressed form) is accomplished with the line:
  44.         TempStream := LoadCompressedResource(RESOURCE_NAME,'');
  45.  
  46.   Part C: Expanding the compressed archive into a directory:
  47.   7. Once the resource is correctly loaded, the TCompress TargetPath
  48.      and MakeDirectories properties are set, then the full archive
  49.      expansion is handled by this line:
  50.         ExpandFilesFromStream(TempStream,nil); { get the lot }
  51.  
  52.   8. Build this program, run it and give it a safe temporary path --
  53.      the files should be expanded to there and, if you've checked the
  54.      boxes, the extracted program should be run and/or the readme file
  55.      viewed with notepad.
  56. *)
  57. interface
  58.  
  59. uses
  60.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  61.   Forms, Dialogs, StdCtrls, DoInstal;
  62.  
  63. type
  64.   TForm1 = class(TForm)
  65.     TargetDirectory: TEdit;
  66.     RunProgram: TCheckBox;
  67.     ViewReadMe: TCheckBox;
  68.     Install: TButton;
  69.     Label1: TLabel;
  70.     procedure FormCreate(Sender: TObject);
  71.     procedure InstallClick(Sender: TObject);
  72.     procedure TargetDirectoryExit(Sender: TObject);
  73.   private
  74.     { Private declarations }
  75.   public
  76.     { Public declarations }
  77.   end;
  78.  
  79. var
  80.   Form1: TForm1;
  81.  
  82. implementation
  83.  
  84. {$R *.DFM}
  85. {$R COMP_RES.RES}
  86.  
  87.  
  88. const PROG_NAME =   'notepad.exe';
  89.       README_NAME = 'readme.txt';
  90.  
  91. procedure TForm1.FormCreate(Sender: TObject);
  92. begin
  93.      TargetDirectory.text := ExtractFilePath(paramstr(0));
  94. end;
  95.  
  96. procedure TForm1.InstallClick(Sender: TObject);
  97. var ReadMeFile, ExeFile: String;
  98. begin
  99.      ExeFile := '';
  100.      ReadMeFile := '';
  101.      if RunProgram.checked then
  102.         ExeFile := PROG_NAME;
  103.      if ViewReadMe.checked then
  104.         ReadMeFile := README_NAME;
  105.      DoInstall(TargetDirectory.text,ExeFile, ReadMeFile);
  106.      ShowMessage('Installation is complete...');
  107.      close;
  108. end;
  109.  
  110. procedure TForm1.TargetDirectoryExit(Sender: TObject);
  111. var path: string;
  112. begin
  113.      path :=TargetDirectory.text;
  114.      if path<>'' then
  115.         if copy(path,Length(path),1)<>'\' then
  116.            TargetDirectory.text := path+'\';
  117. end;
  118.  
  119. end.
  120.  
  121.